package cn.kbyue.exceptiondemo.common.validate;

import cn.kbyue.exceptiondemo.dto.AdviceDTO;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * 根据其他字段的值进行判断校验
 *
 * @author xlang
 * @date 2023/4/4 11:22
 */
public class AdviceGroupSequenceProvider implements DefaultGroupSequenceProvider<AdviceDTO> {
    @Override
    public List<Class<?>> getValidationGroups(AdviceDTO adviceDTO) {
        List<Class<?>> defaultGroupSequence = new ArrayList<>();
        defaultGroupSequence.add(AdviceDTO.class);

        // 这里一定要做判空处理
        if (adviceDTO == null) {
            return defaultGroupSequence;
        }

        String enable = adviceDTO.getEnable();
        // 只有当 enable == 1 时, 才对 AdviceSave 校验组进行校验, 否则不校验
        if ("1".equals(enable)) {
            defaultGroupSequence.add(AdviceSave.class);
        }
        return defaultGroupSequence;
    }
}
